package com.frank.loadinglibrary.common;

import com.frank.loadinglibrary.utils.Indicator;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;

/**
 * 菊花形状加载
 */
public class ChrysantheLoadingCompoment extends BallLoadingComponent implements Component.DrawTask {

    public static HiLogLabel label = new HiLogLabel(HiLog.DEBUG, 1, "TAG");
    //左右边距
    private static final float DEFAULT_MAGIN_HOR = 10f;
    //默认时长基数
    private static final int BASE_DURTION_TIME = 200;
    //动画执行指示器
    protected Indicator indicator;

    private Paint mPaint;

    //设置默认的最大个数
    private static final int DEFAULT_COUNT = 12;

    public float[] alphaFloats;
    float mAlphaSpace = 0f;

    private float mPadding = DEFAULT_MAGIN_HOR;
    //用于刷新的次数统计，每7次清零，用于做透明度变化
    private int reflushCount = 0;
    //绘制线长
    private float mLineLength = 70;
    //绘制线的宽度
    private float mLineWidth = 15;


    public ChrysantheLoadingCompoment(Context context) {
        this(context, null);
    }

    public ChrysantheLoadingCompoment(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    public ChrysantheLoadingCompoment(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init();
    }

    public ChrysantheLoadingCompoment(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        init();
    }

    private void init() {
        HiLog.info(label, "init");
        setPaint();
        setBallCount(DEFAULT_COUNT);
        setDurtion(BASE_DURTION_TIME);
        addDrawTask(this);
        setBindStateChangedListener(new BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                if (autoStart) {
                    startAnimators();
                }
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });

        indicator = new Indicator<AnimatorValue>() {
            @Override
            public ArrayList<AnimatorValue> onCreateAnimators() {
                return ChrysantheLoadingCompoment.this.onCreateAnimators();
            }
        };
    }

    public ChrysantheLoadingCompoment setLineLength(float mLineLength) {
        this.mLineLength = mLineLength;
        return this;
    }

    public ChrysantheLoadingCompoment setLineWidth(float mLineWidth) {
        this.mLineWidth = mLineWidth;
        return this;
    }

    private ArrayList<AnimatorValue> onCreateAnimators() {
        ArrayList<AnimatorValue> animators = new ArrayList<>();
        AnimatorValue alphaAnim = new AnimatorValue();
        alphaAnim.setDuration(durtion);
        alphaAnim.setLoopedCount(-1);
        alphaAnim.setLoopedListener(animator -> ChrysantheLoadingCompoment.this.invalidate());
        indicator.addUpdateListener(alphaAnim, (animatorValue, v) -> {

        });

        animators.add(alphaAnim);
        return animators;
    }

    @Override
    public BaseLoadingComponent setBallCount(int ballCount) {

        super.setBallCount(ballCount);
        HiLog.info(label, "setBallCount " + ballCount);
        alphaFloats = new float[getBallCount()];
        mAlphaSpace = 0.8f / (getBallCount() - 1);
        for (int i = 0; i < getBallCount(); i++) {
            alphaFloats[i] = (float) (0.1 + i * mAlphaSpace);
        }
        return this;
    }

    @Override
    protected void stopAnimators() {
        if (indicator != null) {
            indicator.stop();
        }
    }

    @Override
    protected void startAnimators() {
        if (indicator != null) {
            indicator.start();
        }

    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        HiLog.info(label, "onDraw ");
        int width = Math.min(getWidth(), getHeight());
        for (int i = 0; i < getBallCount(); i++) {
            int position = (i + reflushCount) % getBallCount();
            mPaint.setAlpha(alphaFloats[position]);
            canvas.drawLine(getWidth() / 2 - width / 2 + mPadding, getHeight() / 2, getWidth() / 2 - width / 2 + mPadding + mLineLength, getHeight() / 2, mPaint);
            canvas.rotate(360f / getBallCount(), getWidth() / 2, getHeight() / 2);
        }
        reflushCount++;
        if (reflushCount >= getBallCount() - 1) {
            reflushCount = 0;
        }
    }


    private void setPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setStrokeWidth(mLineWidth);
        mPaint.setColor(Color.WHITE);
    }
}
